<?php
/**
 * 简历管理
 */

class EAdmin_Page_ResumeManage extends EAdmin_Page_Abstract
{
	private $resumeDb;
	private function loadDb()
	{
		$this->resumeDb = Db_Resume::instance();
	}
	public function validate(ME_Request $input, ME_Response $output)
	{
		$euserInfo = Libs_EUser::getLoginStatus($input,$output);
		$output->euid = 
			$input->euid =
			$euid = $euserInfo[0];

		if(empty($euid)){
			header("location:index.php?c=login");
		}

		$state = ME_Config::get("ResumeState");
		$output->state = $state['state'];
		$output->eduDegree = $eduDegree = $state['eduDegree'];

		$dataArr = Libs_Admin_Category::getCategoryList(array('module_type'=>1,'subclassid'=>0,'type'=>1,'pagesize'=>1000));
		$newDataArr = array();
		if($dataArr){
			foreach($dataArr as $data){
				$id = !empty($data['id']) ? $data['id'] : 0;
				if($id == 0){
					continue;
				}
				$newDataArr[$id] = $data['name'];
			}
		}
		$output->cityArr = $newDataArr;


		$stateinfo = Libs_EState::getEResumeState($euid);
		if(!empty($stateinfo)){
			$state_arr = explode(",",$stateinfo);
		}else{
			$state_arr = array();
		}

		$output->state_arr = $state_arr;

		$placeList = Libs_EUser::GetEPlace(array("eid"=>$euid));
		$output->placeList = $placeList['list'];

		return true;
	}

	public function doDefault(ME_Request $input, ME_Response $output)
	{
		$username = $input->post('username');
		$page = $input->get("page");
		if(empty($page)){
			$page = 1;
		}

		$condition = '';

		if(!empty($username)){
			$condition .= ' and trueName="'.$username.'" ';
		}
		$username = !empty($username) ? $username : '';

		$resumeDb = Db_Resume::instance();

		$startnum = ($page-1) * 20;
		$sql = "select * from resume_baseInfo as b, e_resume_job_relation as r where b.uid=r.uid and r.firm_id={$input->euid} and r.is_primary=1 limit {$startnum}, 20";
		$baseinfoList = $resumeDb->getAll($sql);

		$dataList = array();
		$uid_str = "";
		if($baseinfoList){
			foreach($baseinfoList as $datainfo){
				$uid = $datainfo['uid'];
				$uid_str .= $uid.",";
				$dataList[$uid] = $datainfo;
			}	
		}
		$uid_str = trim($uid_str,",");

		$output->dataList = $dataList;

		$count = $resumeDb->getOne("select count('X') from resume_baseInfo where 1 {$condition}");

		$pagestring = Libs_GlobalFunc::simplePage(array("page"=>$page, "pageSize"=>20, "total"=>$count, "baseUrl"=>"index.php?c=ResumeManage&page="));
		$output->pagestring = $pagestring;

		if($uid_str){
			$eduList = array();
			$commonOption = array(
					'db'        => $resumeDb,
					'fromSql'   => 'from resume_edu',
					'whereSql'  => "where uid in({$uid_str})",
					'onlyData'  => true,
					'debug'     => false,
					);
			$eduList = Libs_DBHelper::getList($commonOption);
			$newEduList = array();
			if($eduList){
				foreach($eduList as $eduinfo){
					$edu_uid = $eduinfo['uid'];
					$newEduList[$edu_uid] = $eduinfo;
				}	
			}
			$output->eduList = $newEduList;

			$scoreList = array();
			$commonOption = array(
					'db'		=> $resumeDb,
					'fromSql'	=>'from e_resume_score',
					'whereSql'	=> "where eid={$input->euid} and uid in({$uid_str})",
					'onlyData'	=> true,
					'debug'		=> false
					); 
			$scoreList = Libs_DBHelper::getList($commonOption);
			$newScoreList = array();
			if($scoreList){
				foreach($scoreList as $scoreinfo){
					$s_uid = $scoreinfo['uid'];
					$newScoreList[$s_uid] = $scoreinfo;
				}
			}
			$output->scoreList = $newScoreList;

			$condition = array(
					'db'		=> $resumeDb,
					'fromSql'	=>'from e_job',
					'whereSql'	=> "where firm_id={$input->euid}",
					'onlyData'	=> true,
					'debug'		=> false
					); 
			$joblist = Libs_DBHelper::getList($condition);
		}

		$output->setTemplate("ResumeManage");
	}

	public function doSearch(ME_Request $input, ME_Response $output)
	{
		$page = $input->get("page");
		if(empty($page)){
			$page = 1;
		}

		$output->username = 
			$username = $input->post('username');
		if(!empty($username)){
			$condition .= ' and b.trueName like "%'.$username.'%" ';
		}

		$output->sex = 
			$sex = $input->post('sex');
		if(!empty($sex)){
			$condition .= ' and b.sex='.$sex;
		}

		$output->degree = 
			$degree = $input->post('degree');
		if(!empty($degree)){
			$condition .= ' and e.degree='.$degree;
		}

		$output->schoolname = 
			$schoolname = $input->post('schoolname');
		if(!empty($schoolname)){
			$condition .= ' and e.schoolName like "%'.$schoolname.'%"';
		}

		$output->majorname = 
			$majorname = $input->post('majorname');
		if(!empty($majorname)){
			$condition .= ' and e.majorName like "%'.$majorname.'%"';
		}

		$output->birthProv = 
			$birthProv = $input->post('birthProv');
		if(!empty($birthProv))
		{
			$condition .= ' and b.birthProv='.$birthProv;
		}

		$resumeDb = Db_Resume::instance();

		$startnum = ($page - 1) * 20;
		$sql = "select * from resume_baseInfo as b, resume_edu as e where b.uid=e.uid {$condition} limit {$startnum}, 20";
		$baseinfoList = $resumeDb->getAll($sql);

		/**
		  $startnum = ($page - 1) * 20;
		  $sql = "select * from resume_baseInfo as b, e_resume_job_relation as r where b.uid=r.uid and r.firm_id={$input->euid} and r.is_primary=1 {$condition} limit {$startnum}, 20";
		  $baseinfoList = $resumeDb->getAll($sql);
		 **/
		$dataList = array();
		$uid_str = "";
		if($baseinfoList){
			foreach($baseinfoList as $datainfo){
				$uid = $datainfo['uid'];
				$uid_str .= $uid.",";
				$dataList[$uid] = $datainfo;
			}	
		}
		$uid_str = trim($uid_str,",");

		$output->dataList = $dataList;

		if($uid_str){
			$eduList = array();
			$commonOption = array(
					'db'        => $resumeDb,
					'fromSql'   => 'from resume_edu',
					'whereSql'  => "where uid in({$uid_str})",
					'onlyData'  => true,
					'debug'     => false,
					);
			$eduList = Libs_DBHelper::getList($commonOption);
			$newEduList = array();
			if($eduList){
				foreach($eduList as $eduinfo){
					$edu_uid = $eduinfo['uid'];
					$newEduList[$edu_uid] = $eduinfo;
				}	
			}
			$output->eduList = $newEduList;

			$scoreList = array();
			$commonOption = array(
					'db'		=> $resumeDb,
					'fromSql'	=>'from e_resume_score',
					'whereSql'	=> "where eid={$input->euid} and uid in({$uid_str})",
					'onlyData'	=> true,
					'debug'		=> false
					); 
			$scoreList = Libs_DBHelper::getList($commonOption);
			$newScoreList = array();
			if($scoreList){
				foreach($scoreList as $scoreinfo){
					$s_uid = $scoreinfo['uid'];
					$newScoreList[$s_uid] = $scoreinfo;
				}
			}
			$output->scoreList = $newScoreList;
		}

		$output->setTemplate("ResumeManage");
	}

	//add
	public function doPlaceAdd(ME_Request $input, ME_Response $output)
	{
		//post
		if($input->post('add')){
			$addParam = '';
			$place_name = trim($input->post('name'));
			$eid = $input->euid;
			
			//add
			$insertId = Libs_EUser::AddEPlace(array('eid'=>$eid, 'name'=>$place_name));
			if($insertId){
				self::showMsg('添加成功','/index.php?c=ResumeManage');
			} else {
				self::showMsg('网络连接失败，请稍候再试','index.php?c=ResumeManage&a=PlaceAdd');
			}
		}

		$output->setTemplate("InterviewAdd");
	}

}
